﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using Data.Model;
using DataAccess.AccountManage;
using DataAccess.BikeManage.logical;
using Ninject;
using WinForm.Helper;
using WinForm.Reports;

namespace WinForm.UserManage
{
    public partial class FrmBackCharge : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IOperate _operate = Kernel.Get<IOperate>();

        public FrmBackCharge()
        {
            InitializeComponent();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                var zq58Logical = new Zq58Logical();
                ResultInfo<CpuAccount> cpuAccount = zq58Logical.ReadAccountInfo();
                if (cpuAccount.ResultNumber == ResultEnum.Success)
                {
                    ResultInfo<Decimal> currentMoney = zq58Logical.QueryPurse();

                    ResultInfo<recharge> rechargeinfo = _operate.GetCurrentRecharge(cpuAccount.Obj.CpuCardNo);

                    if ((currentMoney.ResultNumber == ResultEnum.Success)&&(rechargeinfo.ResultNumber==ResultEnum.Success))
                    {
                        if(rechargeinfo.Obj.balance==currentMoney.Obj)
                        {
                            tbCpuNo.Text = cpuAccount.Obj.CpuCardNo;
                            tbUserName.Text = UtilStr.StrNameReplace(cpuAccount.Obj.DisplayName);
                            tbIdCardNo.Text = UtilStr.StrIDCardReplace(cpuAccount.Obj.IdCardNo);
                            tbCurrentTotalMoney.Text = currentMoney.Obj.ToString();
                            tbCzMoney.Text = rechargeinfo.Obj.amount.ToString();
                            tabCz.Visible = true;
                            tabItem1.Visible = false;
                        }else
                        {
                            errorMessage += "数据库中用户余额与卡中的用户余额不同，请联系运营商";
                        }
                    }
                    else
                    {
                        errorMessage = errorMessage + currentMoney.Describe;
                        errorMessage += rechargeinfo.Describe;
                    }
                }
                else
                {
                    errorMessage = cpuAccount.Describe;
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }

        private void btnCharge_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            try
            {
                var zq58Logical = new Zq58Logical();
                ResultInfo<CpuAccount> userInfo = zq58Logical.ReadAccountInfo();
                if (userInfo.ResultNumber == ResultEnum.Success)
                {
                    decimal cz = Converts.ConvertToDecimal(tbCzMoney.Text);
                    if (cz <= 0)
                    {
                        errorMessage = errorMessage + "充值回退金额要大于0";
                    }
                    else
                    {
                        if(cz>Converts.ConvertToDecimal(tbCurrentTotalMoney.Text))
                        {
                            errorMessage += "充值回退金额不能超过当前用户总金额，请重新确认回退金额！";
                        }else
                        {
                            ResultInfo<byte[]> czMoney = zq58Logical.CostMoney(Converts.ConvertToDecimal(tbCzMoney.Text));
                            if (czMoney.ResultNumber == ResultEnum.Success)
                            {
                                ResultInfo<Decimal> totalMoney = zq58Logical.QueryPurse();
                                if (totalMoney.ResultNumber == ResultEnum.Success)
                                {
                                    lbToltalMoney.Text = totalMoney.Obj.ToString();

                                    ResultInfo<bool> dbR =
                                        _operate.RechargeYourPurse(Converts.ConvertToDecimal(tbCurrentTotalMoney.Text),
                                            -Converts.ConvertToDecimal(tbCzMoney.Text), userInfo.Obj.CpuCardId,
                                            SystemInfo.OperateAddress, SystemInfo.OperateAddressId, SystemInfo.OperatePsamId,
                                            SystemInfo.OperateId);
                                    if (dbR.ResultNumber == ResultEnum.Success)
                                    {
                                        MyMessageBox.ShowMessageBoxInfo(
                                            string.Format("充值回退成功！账户初始金额为：{0}元，本次充值回退：{1}元，当前余额：{2}元",
                                                          tbCurrentTotalMoney.Text, tbCzMoney.Text,
                                                          (Converts.ConvertToDecimal(tbCurrentTotalMoney.Text) -
                                                           Converts.ConvertToDecimal(tbCzMoney.Text))));
                                    }
                                    else
                                    {
                                        ResultInfo<byte[]> costMoney =
                                            zq58Logical.PurseSave(Converts.ConvertToDecimal(tbCzMoney.Text));
                                        if (costMoney.ResultNumber != ResultEnum.Success)
                                        {
                                            errorMessage = errorMessage + costMoney.Describe;
                                        }
                                        errorMessage += dbR.Describe;
                                    }
                                }
                                else
                                {
                                    //ResultInfo<byte[]> costMoney =
                                    //        zq58Logical.PurseSave(Converts.ConvertToDecimal(tbCzMoney.Text));
                                    //if (costMoney.ResultNumber != ResultEnum.Success)
                                    //{
                                    //    errorMessage = errorMessage + costMoney.Describe;
                                    //}
                                    errorMessage = errorMessage + totalMoney.Describe;
                                }
                            }
                            else
                            {
                                errorMessage = errorMessage + "充值回退未成功：" + czMoney.Describe;
                            }
                        }
                    }
                }
                else
                {
                    errorMessage = errorMessage + userInfo.Describe;
                }
            }
            catch (Exception e1)
            {
                errorMessage = errorMessage + e1.Message;
            }


            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }
    }
}